package com.cinemahorairerapido.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.cinemahorairerapido.R;
import com.cinemahorairerapido.bean.Cinema;
import com.cinemahorairerapido.controller.FavorisController;

public class FindCinemaAdapter extends BaseAdapter {

	List<Cinema> cinemaList = new ArrayList<Cinema>();

	Context context;

	LayoutInflater inflater;

	private class ViewHolder {
		TextView nameTextView;
		TextView addressTextView;
		CheckBox favoriteCheckBox;
	}

	public FindCinemaAdapter(List<Cinema> pCinemaList, Context context) {
		super();
		this.cinemaList = pCinemaList;
		this.context = context;

		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return cinemaList.size();
	}

	@Override
	public Object getItem(int position) {
		return cinemaList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return cinemaList.get(position).getId();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;

		if (convertView == null) {

			holder = new ViewHolder();

			convertView = inflater.inflate(R.layout.findcinemarow, null);

			holder.nameTextView = (TextView) convertView.findViewById(R.id.nameFindCinemaRow);
			holder.addressTextView = (TextView) convertView.findViewById(R.id.addressFindCinemaRow);
			holder.favoriteCheckBox = (CheckBox) convertView.findViewById(R.id.checkBoxFavoriteFindCinemaRow);

			convertView.setTag(holder);

		} else {

			holder = (ViewHolder) convertView.getTag();

		}

		final Cinema cinema = cinemaList.get(position);

		holder.nameTextView.setText(cinema.getName());
		holder.addressTextView.setText(cinema.getAddress() + " " + cinema.getPostalCode() + " " + cinema.getCity());

		holder.favoriteCheckBox.setChecked(FavorisController.getInstance(context).isFavorite(cinema));

		holder.favoriteCheckBox.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				final CheckBox cb = (CheckBox) v;

				if (!cb.isChecked()) {
					// when uncheck ask for confirmation
					AlertDialog.Builder builder = new AlertDialog.Builder(context);
					builder.setMessage(context.getResources().getString(R.string.removeconfirmation)).setCancelable(false)
							.setPositiveButton(context.getResources().getString(R.string.yes), new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int id) {
									FavorisController.getInstance(context).updateFavoris(cinema, cb.isChecked());
								}
							}).setNegativeButton(context.getResources().getString(R.string.no), new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog, int id) {
									cb.setChecked(true);
									dialog.cancel();
								}
							});
					AlertDialog alert = builder.create();
					alert.show();

				} else {
					FavorisController.getInstance(context).updateFavoris(cinema, cb.isChecked());
				}

			}
		});

		return convertView;
	}

}